#!/bin/bash

if [[ $(id -u) -ne 0 ]]; then
  echo "This script must be executed as root or using sudo."
  exit 99
fi

plugin_list="
docker-ce
docker-ce-cli
docker-compose-plugin
openmediavault-apttool
openmediavault-autoshutdown
openmediavault-backup
openmediavault-borgbackup
openmediavault-usbbackup
openmediavault-clamav
openmediavault-fail2ban
openmediavault-resetperms
openmediavault-diskstats
openmediavault-downloader
openmediavault-mergerfs
openmediavault-remotemount
openmediavault-rsnapshot
openmediavault-filebrowser
openmediavault-flashmemory
openmediavault-sftp
openmediavault-sharerootfs
openmediavault-symlinks
openmediavault-wakealarm
openmediavault-wol
openmediavault-lvm2
openmediavault-zfs 
openmediavault-snapraid
openmediavault-s3
openmediavault-tgt
openmediavault-compose 
openmediavault-kvm
openmediavault-kernel
"


cat <<EOF > /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main

deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
EOF

cat <<EOF > /etc/apt/sources.list.d/omvdocker.list
deb [signed-by=/usr/share/keyrings/docker.gpg arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian bullseye stable
EOF

cat <<EOF > /etc/apt/sources.list.d/omvextras.list
deb [signed-by=/usr/share/keyrings/omvextras.gpg arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/openmediavault-plugin-developers shaitan main
EOF

cat <<EOF > /etc/apt/sources.list.d/openmediavault.list
deb https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/public shaitan main
deb https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/packages shaitan main
## Uncomment the following line to add software from the proposed repository.
# deb https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/public shaitan-proposed main
# deb https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/packages shaitan-proposed main
## This software is not part of OpenMediaVault, but is offered by third-party
## developers as a service to OpenMediaVault users.
# deb https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/public shaitan partner
# deb https://mirrors.tuna.tsinghua.edu.cn/OpenMediaVault/packages shaitan partner
EOF

cat <<EOF > /etc/apt/sources.list.d/openmediavault-os-security.list
# default 
# deb http://security.debian.org/debian-security bullseye-security main contrib non-free
# deb-src http://security.debian.org/debian-security bullseye-security main contrib non-free
#
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
EOF

apt clean all
apt update 
apt  -y  upgrade
wait
apt  -y install  ${plugin_list}
wait

[ ! -f /etc/docker ]&& mkdir -p  /etc/docker
wait
cat > /etc/docker/daemon.json << 'EOF'
{
    "data-root": "/var/lib/docker",
    "registry-mirrors": [
        "https://registry.docker-cn.com",
        "https://hub-mirror.c.163.com",
        "https://ccr.ccs.tencentyun.com",
        "https://docker.mirrors.ustc.edu.cn"
    ]
}
EOF

# set timezone Asia/Shanghai
timedatectl set-timezone Asia/Shanghai

systemctl daemon-reload

systemctl restart docker
